package com.example.common.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.example.common.utils.StringUtils;

/**
 * @Author 王鹤蒲
 * @Date 2023 10 14
 */
public class DelFlagConverter implements Converter<String> {
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value=cellData.getStringValue();
        if(StringUtils.isEmpty(value)){
            return null;
        }
        if("存在".equals(value)){
            return "0";
        }else if("删除".equals(value)){
            return "1";
        }else{
            return null;
        }
    }

    public WriteCellData convertToExcelData(String value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value== null) {
            return new WriteCellData<>("");
        }
        if ("0".equals(value)) {
            return new WriteCellData<>("存在");
        } else if ("1".equals(value)) {
            return new WriteCellData<>("删除");
        } else{
            return new WriteCellData<>("");
        }
    }
}
